<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-03-01
 * Time: 14:07
 */

namespace iBrand\Store\Backend\Console;

use iBrand\AlbumBackend\Models\ImageCategory;
use iBrand\Backend\Models\Admin;
use iBrand\Component\Product\Models\Supplier;
use iBrand\Store\Backend\Model\CategoryGroup;
use Illuminate\Console\Command;
//use Spatie\Permission\Models\Role;
use iBrand\Cms\Models\Category;
use iBrand\Store\Backend\Model\Category as ElCategory;

class SetDefaultValueCommand extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'dmp-store:default-value';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'install dmp\'s store system default value.';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		//1. 设置站点信息默认值
		if (empty(settings()->getSetting('store_name'))) {
			settings()->setSetting(['store_name' => 'ibrand']);
		}

		if (empty(settings()->getSetting('menu_list'))) {
			settings()->setSetting(['menu_list' => [
				['name' => '商城', 'url' => '', 'sort' => 1, 'is_enabled' => 1, 'image_url' => ['front' => url('assets/backend/images/tb_icon_0.png'), 'after' => url('assets/backend/images/tb_icon_0_a.png')]]
				/*, ['name' => '体验', 'url' => '', 'sort' => 1, 'is_enabled' => 1, 'image_url' => ['front' => '', 'after' => '']]
				, ['name' => '发现', 'url' => '', 'sort' => 1, 'is_enabled' => 1, 'image_url' => ['front' => '', 'after' => '']]*/
				, ['name' => '门店', 'url' => '', 'sort' => 1, 'is_enabled' => 1, 'image_url' => ['front' => url('assets/backend/images/tb_icon_3.png'), 'after' => url('assets/backend/images/tb_icon_3_a.png')]]
				, ['name' => '我', 'url' => '', 'sort' => 1, 'is_enabled' => 1, 'image_url' => ['front' => url('assets/backend/images/tb_icon_4.png'), 'after' => url('assets/backend/images/tb_icon_4_a.png')]],
			]]);
		}

		/*if (!Role::where('name', 'administrator')->first()) {
			Role::create(['name' => 'administrator']);
		}

		if (!Role::where('name', 'manager')->first()) {
			Role::create(['name' => 'manager']);
		}

		if (!Role::where('name', 'kefu')->first()) {
			Role::create(['name' => 'kefu']);
		}*/

		if (!Admin::where('email', 'yaochao@admin.com')->first()) {
			Admin::create(['name' => 'yaochao@admin.com', 'email' => 'yaochao@admin.com', 'mobile' => '13548640109', 'password' => '123456']);
		}

		if (!Admin::where('email', 'tracy@iyoyo.cc')->first()) {
			Admin::create(['name' => 'tracy@iyoyo.cc', 'email' => 'tracy@iyoyo.cc', 'mobile' => '13816704762', 'password' => '123456']);
		}

		/*if ($user = Admin::where('name', 'admin')->first() AND !$user->hasRole('administrator')) {
			$user->assignRole('administrator');
		}

		if ($user = Admin::where('email', 'yaochao@admin.com')->first() AND !$user->hasRole('administrator')) {
			$user->assignRole('administrator');
		}

		if ($user = Admin::where('email', 'tracy@iyoyo.cc')->first() AND !$user->hasRole('administrator')) {
			$user->assignRole('administrator');
		}*/

		if (empty(settings()->getSetting('wechat_app_id'))) {
			settings()->setSetting(['wechat_app_id' => 'wx8f53755de2d1e0cc']);
		}

		if (empty(settings()->getSetting('wechat_api_url'))) {
			settings()->setSetting(['wechat_api_url' => 'http://wecom.ibrand.cc/']);
		}

		if (empty(settings()->getSetting('wechat_api_client_id'))) {
			settings()->setSetting(['wechat_api_client_id' => '3']);
		}

		if (empty(settings()->getSetting('wechat_api_client_secret'))) {
			settings()->setSetting(['wechat_api_client_secret' => '03ZsUGy8w5odcS9WifDuJu1c7jDMOid0BMKg6jLd']);
		}

		if (empty(settings()->getSetting('goods_spec_color'))) {
			settings()->setSetting(['goods_spec_color' => ['黑色',
			                                               '绿色',
			                                               '白色',
			                                               '紫色',
			                                               '红色',
			                                               '黄色',
			                                               '蓝色',
			                                               '棕色',
			                                               '灰色']]);
		}
		//dd(settings()->getSetting('point_enabled') );
		$point_enabled = settings()->getSetting('point_enabled');
		if (empty($point_enabled)) {
			settings()->setSetting(['point_enabled' => 0]);
		}

		//前端是否显示库存
		$shop_show_store = settings()->getSetting('shop_show_store');
		if (empty($shop_show_store)) {
			settings()->setSetting(['shop_show_store' => 0]);
		}

		if (!Category::where('name', '测试')->first()) {
			Category::create(['name' => '测试', 'created_at' => date('Y-m-d H:i:s', time()), 'updated_at' => date('Y-m-d H:i:s', time())]);
		}

		if (!Category::where('name', '指南')->first()) {
			Category::create(['name' => '指南', 'created_at' => date('Y-m-d H:i:s', time()), 'updated_at' => date('Y-m-d H:i:s', time())]);
		}

		if (!ImageCategory::where('name', '默认分组')->first()) {
			ImageCategory::create(['parent_id' => 0, 'name' => '默认分组', 'sort' => 0]);
		}

		if (!Supplier::where('name', '商家自营')->where('id', 1)->first()) {
			Supplier::create(['name' => '商家自营', 'code' => 'SELF', 'status' => 1]);
		}

		$group = CategoryGroup::all();
		if (count($group) <= 0) {
			CategoryGroup::create([
				'group_name'  => '默认分组',
				'description' => '默认分组',
			]);
		}

		$category = ElCategory::all();
		if (count($category) <= 0) {
			ElCategory::create([
				'group_id'    => 1,
				'name'        => '默认分类',
				'description' => '默认分类',
				'sort'        => 1,
				'status'      => 1,
				'level'       => 1,
				'parent_id'   => 0,
			]);
		}

		$menu          = config('admin.database.menu_model');
		$category_menu = $menu::where('title', '分类管理')->where('uri', 'store/category_group')->first();
		if ($category_menu) {
			$category_menu->uri = 'store/category';

			$category_menu->save();
		}
		/*$this->call('passport:keys');
		$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
		$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client']);*/
	}
}
